package com.usian.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Author:张永杰
 * @ClassName: GloableCorsConfig
 * @Date: 2023/9/4
 * @Description:
 */
@Configuration
public class GloableCorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        //添加cors的配置信息
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        //是否发送cookie
        config.setAllowCredentials(true);
        //有效时间
        config.setMaxAge(3600l);
        //添加映射路径，拦截一切请求
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration("/**",config);
        //返回过滤器
        return new CorsWebFilter(configSource);

    }
}
